package com.ruoyi.web.controller.gcgl;

import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gcgl.domain.GcProject;
import com.ruoyi.gcgl.domain.GcProjectSbclht;
import com.ruoyi.gcgl.domain.GcProjectSbclhtjhd;
import com.ruoyi.gcgl.service.IGcProjectSbclhtService;
import com.ruoyi.gcgl.service.IGcProjectSbclhtjhdService;
import com.ruoyi.gcgl.service.IGcProjectService;

/**
 * 设备材料合同交货单Controller
 * 
 * @author ruoyi
 * @date 2022-12-03
 */
@Controller
@RequestMapping("/gcgl/sbclhtjhd")
public class GcProjectSbclhtjhdController extends BaseController{
    private String prefix = "gcgl/sbclhtjhd";

    @Autowired
    private IGcProjectSbclhtjhdService gcProjectSbclhtjhdService;
    @Autowired
    private IGcProjectSbclhtService gcProjectSbclhtService;
    @Autowired
    private IGcProjectService gcProjectService;

    @RequiresPermissions("gcgl:sbclhtjhd:view")
    @GetMapping()
    public String sbclhtjhd(){
        return prefix + "/sbclhtjhd";
    }

    /**
     * 查询设备材料合同交货单列表
     */
    @RequiresPermissions("gcgl:sbclhtjhd:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GcProjectSbclhtjhd gcProjectSbclhtjhd){
        startPage();
        if(getSysUser().isShow43R(getUserId())){
        gcProjectSbclhtjhd.setCreateUser(getLoginName());
        }
        gcProjectSbclhtjhd.setDeleteFlag(1);
        List<GcProjectSbclhtjhd> list = gcProjectSbclhtjhdService.selectGcProjectSbclhtjhdList(gcProjectSbclhtjhd);
        return getDataTable(list);
    }

    /**
     * 导出设备材料合同交货单列表
     */
    @RequiresPermissions("gcgl:sbclhtjhd:export")
    @Log(title = "设备材料合同交货单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GcProjectSbclhtjhd gcProjectSbclhtjhd){
    	gcProjectSbclhtjhd.setDeleteFlag(1);
    	if(getSysUser().isShow()){
            gcProjectSbclhtjhd.setCreateUser(getLoginName());
            }
        List<GcProjectSbclhtjhd> list = gcProjectSbclhtjhdService.selectGcProjectSbclhtjhdList(gcProjectSbclhtjhd);
        ExcelUtil<GcProjectSbclhtjhd> util = new ExcelUtil<GcProjectSbclhtjhd>(GcProjectSbclhtjhd.class);
        return util.exportExcel(list, "设备材料合同交货单数据");
    }

    /**
     * 新增设备材料合同交货单
     */
    @GetMapping("/add/{sbclhtId}")
    public String add(@PathVariable("sbclhtId") Long sbclhtId, ModelMap mmap){
    	GcProjectSbclht gcProjectSbclht = gcProjectSbclhtService.selectGcProjectSbclhtById(sbclhtId);
        mmap.put("gcProjectSbclht", gcProjectSbclht);
        
        GcProject gcProject = gcProjectService.selectGcProjectById(gcProjectSbclht.getGcProjectid());
    	mmap.put("gcProject", gcProject);
        return prefix + "/add";
    }

    /**
     * 新增保存设备材料合同交货单
     */
    @RequiresPermissions("gcgl:sbclhtjhd:add")
    @Log(title = "设备材料合同交货单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GcProjectSbclhtjhd gcProjectSbclhtjhd){
    	
    	try{
            gcProjectSbclhtjhd.setDeleteFlag(1);
            gcProjectSbclhtjhd.setCreateAt(new Date());
            gcProjectSbclhtjhd.setUpdateAt(new Date());
            gcProjectSbclhtjhd.setCreateUser(getLoginName());
            gcProjectSbclhtjhd.setUpdateUser(getLoginName());
            gcProjectSbclhtjhdService.insertGcProjectSbclhtjhd(gcProjectSbclhtjhd);
        }
        catch (Exception e){
            return error(e.getMessage());
        }
    	
        return toAjax(1);
    }

    /**
     * 修改设备材料合同交货单
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap){
        GcProjectSbclhtjhd gcProjectSbclhtjhd = gcProjectSbclhtjhdService.selectGcProjectSbclhtjhdById(id);
        if(gcProjectSbclhtjhd.getZilwj()!=null && !gcProjectSbclhtjhd.getZilwj().equals("")){
        	String[] zlis = gcProjectSbclhtjhd.getZilwj().split(",");
        	String file = "";
        	for(int i=0;i<zlis.length;i++){
        		file += RuoYiConfig.getOnPath()+zlis[i]+",";
        	}
        	gcProjectSbclhtjhd.setZilwj(file);
        }
        mmap.put("gcProjectSbclhtjhd", gcProjectSbclhtjhd);
        
        GcProjectSbclht gcProjectSbclht = 
        		gcProjectSbclhtService.selectGcProjectSbclhtById(gcProjectSbclhtjhd.getSbclhtId());
        mmap.put("gcProjectSbclht", gcProjectSbclht);
        
        GcProject gcProject = gcProjectService.selectGcProjectById(gcProjectSbclht.getGcProjectid());
    	mmap.put("gcProject", gcProject);
        return prefix + "/edit";
    }

    /**
     * 修改保存设备材料合同交货单
     */
    @Log(title = "设备材料合同交货单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GcProjectSbclhtjhd gcProjectSbclhtjhd){
    	try{
            gcProjectSbclhtjhdService.updateGcProjectSbclhtjhd(gcProjectSbclhtjhd);
        }
        catch (Exception e){
            return error(e.getMessage());
        }
    	
        return toAjax(1);
    }

    /**
     * 删除设备材料合同交货单
     */
    @RequiresPermissions("gcgl:sbclhtjhd:remove")
    @Log(title = "设备材料合同交货单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids){
        return toAjax(gcProjectSbclhtjhdService.deleteGcProjectSbclhtjhdByIds(ids));
    }
}